#pragma once
//文件版本

//model：用于和数据进行交互，对外提供访问数据的接口！
//根据题目list文件，将所有题目信息加载到内存中！

#include <iostream>
#include <string>
#include <unordered_map>
#include <cassert>
#include <vector>
#include <fstream>
#include <cstdlib>
#include "../comm/log.hpp"
#include "../comm/util.hpp"
namespace ns_model
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;
    //问题结构体
    struct Question
    {
        std::string number; //唯一题目编号
        std::string title;  //题目的标题
        std::string star;   //题目的难度等级 (简单 中等 困难)
        int cpu_limit;      //题目的时间要求 
        int mem_limit;      //题目的空间要求

        std::string desc;   //题目的描述
        std::string header; //题目给用户预设的代码
        std::string tail;   //题目的测试用例，需要与header拼接形成完整代码
    };

    const std::string questions_list = "./questions/questions.list"; //题目列表路径
    const std::string questions_path = "./questions/"; //具体的题目路径,根据具体题目改变

    class Model
    {
    public:
        Model() 
        {
            assert(LoadQuestions(questions_list));
        }

        ~Model() {}

        bool LoadQuestions(const std::string& question_list)
        {
            //加载配置文件。路径： questions/questions.list + 题目编号文件
            ifstream in(question_list);
            if(!in.is_open())
            {
                LOG(FATAL) << "加载题库失败，请检查是否存在题库对应文件！" << std::endl;
                return false;
            }

            std::string line;
            while(std::getline(in,line))
            {
                std::vector<std::string> tokens;
                StringUtil::SplitString(line,&tokens," ");
                
                if(tokens.size() != 5)
                {
                    LOG(WARNING) << "加载部分题目失败，请检查文件格式" << endl;
                    continue;
                }
                //std::cout << tokens[0] << " " << tokens[1] << " "<< tokens[2] << " "
                //<< tokens[3] << " " << tokens[4] << " ";
                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = atoi(tokens[3].c_str());
                q.mem_limit = atoi(tokens[4].c_str());

                std::string path = questions_path;
                path += q.number;
                path += "/";
 
                FileUtil::ReadFile(path+"desc.txt",&(q.desc),true);
                FileUtil::ReadFile(path+"header.cpp",&(q.header),true);
                FileUtil::ReadFile(path+"tail.cpp",&(q.tail),true);

                //for debug
                //std::cout << q.number << std::endl << q.desc << std::endl << q.header << endl;
                questions.insert({q.number,q});
            }
            LOG(INFO) << "加载题库成功!" << endl;
            in.close();

            return true;
        }

        bool GetAllQuestions(vector<Question>* out)
        {
            if(questions.size() == 0)
            {
                LOG(ERROR) << "用户获取全部题库失败！ " << endl;
                return false;
            }
            
            for(const auto& q : questions)
            {
                out->push_back(q.second);
            }
            return true;
        }

        bool GetOneQuestion(const std::string& number,Question* q)
        {
            const auto& iter = questions.find(number);
            if(iter == questions.end())
            {
                LOG(ERROR) << "用户获取题目" << number << "失败！ " << endl;
                return false;
            }
            (*q) = iter->second;
            return true;
        }   
    private:
        //题号映射到题目细节的hash_map
        unordered_map<std::string,Question> questions;
    };
}